#include "screenutil.h"

#include <QGuiApplication>
#include <QScreen>
#include <QWindow>

/**
 * 获取屏幕物理像素与设备独立像素（逻辑像素）之间的比例。
 * 1. 处理多屏时 windowHandle 有效，并使用其 screen
 * 2. 在单屏时使用 QGuiApplication::primaryScreen 。
 * @brief ScreenUtil::ratio
 * @param widget
 * @return 像素比例
 */
qreal ScreenUtil::ratio(QWidget *widget)
{
    qreal ratio = 1;

    QWindow *window = widget ? widget->windowHandle(): nullptr;
    QScreen *screen = nullptr;

    screen = window ? window->screen()
             : QGuiApplication::primaryScreen();;

    ratio = screen ? screen->devicePixelRatio()
            : 1;

    return ratio;
}

/**
 * @brief  移动到指定的屏幕中心、靠上、左上、右上、左下、右下
 * @note
 * @param  &window:
 * @param  *screen:
 * @retval None
 */
void ScreenUtil::moveScreenCenter(QWidget &window, QScreen *screen)
{
    QRect screenRect = screen->geometry();

    //  获取屏幕的位置信息，计算出相对位置
    int x = screenRect.x();
    int y = screenRect.y();
    int width = screenRect.width();
    int height = screenRect.height();

    // 补尝偏移
    int wm_x = (width - window.width()) / 2 ;
    int wm_y = (height - window.height()) / 2 ;

    window.move(x + wm_x, y + wm_y);
}

void ScreenUtil::moveScreenCenterMiddleUp(QWidget &window, QScreen *screen)
{
    QRect screenRect = screen->geometry();

    //  获取屏幕的位置信息，计算出相对位置
    int x = screenRect.x();
    int y = screenRect.y();
    int width = screenRect.width();
    int height = screenRect.height();

    // 补尝偏移
    int retaste_x = (width - window.width()) / 2;
    int retaste_y = height / 4;

    window.move(x + retaste_x, y + retaste_y);
}

void ScreenUtil::moveScreenTopLeft(QWidget &window, QScreen *screen)
{
    QRect screenRect = screen->geometry();

    //  获取屏幕的位置信息，计算出相对位置
    int x = screenRect.x();
    int y = screenRect.y();
    int width = screenRect.width();
    int height = screenRect.height();

    // 补尝偏移
    int retaste_x = 0;
    int retaste_y = 0;

    window.move(x + retaste_x, y + retaste_y);
}

void ScreenUtil::moveScreenTopCenter(QWidget &window, QScreen *screen)
{
    QRect screenRect = screen->geometry();

    //  获取屏幕的位置信息，计算出相对位置
    int x = screenRect.x();
    int y = screenRect.y();
    int width = screenRect.width();
    int height = screenRect.height();

    // 补尝偏移
    int retaste_x = (width - window.width()) / 2;
    int retaste_y = 0;

    window.move(x + retaste_x, y + retaste_y);
}

void ScreenUtil::moveScreenTopRight(QWidget &window, QScreen *screen)
{
    QRect screenRect = screen->geometry();

    //  获取屏幕的位置信息，计算出相对位置
    int x = screenRect.x();
    int y = screenRect.y();
    int width = screenRect.width();
    int height = screenRect.height();

    // 补尝偏移
    int retaste_x = (width - window.width());
    int retaste_y = 0;

    window.move(x + retaste_x, y + retaste_y);
}

void ScreenUtil::moveScreenBottomRight(QWidget &window, QScreen *screen)
{
    QRect screenRect = screen->geometry();

    //  获取屏幕的位置信息，计算出相对位置
    int x = screenRect.x();
    int y = screenRect.y();
    int width = screenRect.width();
    int height = screenRect.height();

    // 补尝偏移
    int retaste_x = width - window.width();
    int retaste_y = height - window.height();

    window.move(x + retaste_x, y + retaste_y);
}

void ScreenUtil::moveScreenBottomLeft(QWidget &window, QScreen *screen)
{
    QRect screenRect = screen->geometry();

    //  获取屏幕的位置信息，计算出相对位置
    int x = screenRect.x();
    int y = screenRect.y();
    int width = screenRect.width();
    int height = screenRect.height();

    // 补尝偏移
    int retaste_x = 0;
    int retaste_y = height - window.height();

    window.move(x + retaste_x, y + retaste_y);
}

void ScreenUtil::moveCenter(QWidget &window)
{
    if (QGuiApplication::screens().length() == 1)
    {
        movePrimaryScreenCenter(window);
    }
    else
    {
        moveScreenCenter(window, window.windowHandle()->screen());
    }
}

void ScreenUtil::moveCenterMiddleUp(QWidget &window)
{
    moveScreenCenterMiddleUp(window, window.windowHandle()->screen());
}

void ScreenUtil::movePrimaryScreenCenter(QWidget &window)
{
    QScreen *screen = QGuiApplication::primaryScreen();  // 主屏幕
    moveScreenCenter(window, screen);
}

void ScreenUtil::moveCenterForCursor(QWidget &window)
{
    int cx = QCursor::pos().x();
    int cy = QCursor::pos().y();

    // 对鼠标进行一次屏幕轮询识别在哪个屏幕上
    for (QScreen *screen : QGuiApplication::screens())
    {
        int x = screen->geometry().x();
        int y = screen->geometry().y();
        int width = screen->geometry().width();
        int height = screen->geometry().height();

        // 鼠标范围检查规则，在屏幕坐标原点第四象限范围
        // 1. 向下不超过屏幕所在位置的高度
        // 2. 向右不超过屏幕所在位置的宽度
        if ((cy > y && cy < y + height) && (cx > x && cx < x + width))
        {
            moveScreenCenter(window, screen);
        }
    }
}
